package com.changgou.orderWeb.controller;

import com.changgou.entity.Result;
import com.changgou.order.feign.CartFeign;
import com.changgou.orderWeb.config.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("/wcart")
public class CartWebCotroller {

    @Autowired
    private CartFeign cartFeign;

    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private HttpServletRequest request;



    @GetMapping("/list")
    public String list(Model model) {
        Result<Map> itemResult = cartFeign.list();
        Map item = itemResult.getData();
        String authorization = request.getHeader("Authorization");
        if (!StringUtils.isEmpty(authorization)) {
            String userName = tokenDecode.getUserName();
            model.addAttribute("username", userName);
        }
        model.addAttribute("items", item);
        return "cart";
    }

}
